package com.system.service;

import java.util.ArrayList;
import java.util.List;

import com.system.dao.CourseDao;
import com.system.dao.DaoFactory;
import com.system.dao.RemarkDao;
import com.system.dao.TeacherDao;
import com.system.dao.UserDao;
import com.system.json.JsonContext;
import com.system.model.Course;
import com.system.model.Remark;
import com.system.model.Teacher;
import com.system.model.User;

import net.sf.json.JSONObject;
import net.sf.json.JSONArray;

public class TeacherService {
	private DaoFactory factory = DaoFactory.getInstance();
	private JsonContext context = new JsonContext();
	
	//查询所有老师的信息
	public JSONObject queryAll() {
		TeacherDao teaDao = factory.createTeacherDao();
		ArrayList<Teacher> teachers = teaDao.findAll();
		if(teachers==null){
			return context.getFailedObject("老师列表为空");
		}else{
			JSONArray array = new JSONArray();
			for(int i = 0; i < teachers.size(); i++) {
				Teacher tea = teachers.get(i);
				JSONObject json=JSONObject.fromObject(tea);
				array.add(json);
			}
			return context.getSuccessObject("查询成功", null, array);
		}
	}
	/**
	 * 查询老师信息并且带分页效果
	 * @param pageNum
	 * @param amount
	 * @return
	 */
	public JSONObject checkoutQueryall(int pageNum,int amount){
		TeacherDao tdao=factory.createTeacherDao();
		List<Teacher> teachers=tdao.findAll(pageNum, amount);
		if(teachers==null)
			return context.getFailedObject("查询失败，请提供正确的参数");
		else{
			JSONArray arr=JSONArray.fromObject(teachers);			
			return context.getSuccessObject("查询成功", null, arr);
		}
	}
	
	//根据user_id来查询老师信息
	public JSONObject queryInfo(String user_id) {
		TeacherDao teaDao = factory.createTeacherDao();
		Teacher tea = teaDao.findById(user_id);
		if(tea==null){
			return context.getFailedObject("该老师不存在");
		}else{
			JSONObject json=JSONObject.fromObject(tea);
			return context.getSuccessObject("查询成功", json, null);
		}
	}
	//添加老师信息
	//【注】注意外键约束
	public JSONObject add(Teacher tea,String coursename) {
		TeacherDao teaDao = factory.createTeacherDao();
		UserDao userDao = factory.createUserDao();
		CourseDao courseDao=factory.createCourseDao();
		User user = new User();
		user.setUsername(tea.getTeanum());
		user.setPassword("123456");
		user.setPost("teacher");
		boolean res = false;
		res = userDao.add(user);
		if (!res) {
			//在user表中重复
			return context.getFailedObject("该老师已注册");
		}
		
		try {
			user = userDao.findByUsername(tea.getTeanum());
		} catch (Exception e) {
			e.printStackTrace();
		}
		tea.setUser_id(user.getId());
		res = teaDao.add(tea);
		Course course=new Course();course.setTeacher_id(teaDao.findId(tea.getTeanum()));
		course.setCoursename(coursename);
		courseDao.add(course);

		if(!res){
			return context.getFailedObject("添加老师信息失败");
		}else{
			return context.getSuccessObject("添加老师信息成功", null, null);
		}
	}
	
	//修改老师信息
	public JSONObject modify(Teacher tea) {
		TeacherDao teaDao = factory.createTeacherDao();
		boolean res = teaDao.modify(tea);
		if(!res){
			return context.getFailedObject("修改老师信息失败");
		}else{
			return context.getSuccessObject("修改老师信息成功", null, null);
		}
	}
	//删除老师信息
	public JSONObject deleteTeachers(int[] teaIds) {
		TeacherDao teaDao = factory.createTeacherDao();
		boolean res = teaDao.deleteTeachers(teaIds);
		if(!res){
			return context.getFailedObject("删除老师信息失败");
		}else{
			return context.getSuccessObject("删除老师信息成功", null, null);
		}
	}
	/**
	 * 查看学生反馈
	 * @param teanum
	 * @return
	 */
	public JSONObject checkoutQueryRemarks(String teanum){
		TeacherDao teaDao=factory.createTeacherDao();
		RemarkDao rdao=factory.createRemarkDao();
		int tea_id=teaDao.findId(teanum);
		List<Remark> list=rdao.listRemarks(tea_id);
		if(list==null){
			return context.getFailedObject("查询失败");
		}
		else{
			JSONArray arr=JSONArray.fromObject(list);
			return context.getSuccessObject("查询成功", null, arr);
		}
	}
}